package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/model/do"
	"wh-1-backend/internal/service"
)

type cMenu struct{}

var Menu = cMenu{}

func (m *cMenu) List(ctx context.Context, req *v1.MenuListReq) (res *v1.MenuListRes, err error) {
	total, list, err := service.Menu().List(ctx, req.Page, req.PageSize)
	if err == nil {
		res = &v1.MenuListRes{
			List:  list,
			Total: total,
		}
	}
	return
}

func (m *cMenu) Tree(ctx context.Context, req *v1.MenuTreeReq) (res *v1.MenuTreeRes, err error) {
	tree, err := service.Menu().Tree(ctx)
	if err != nil {
		return nil, err
	}
	res = &v1.MenuTreeRes{Tree: tree}
	return
}

func (m *cMenu) Create(ctx context.Context, req *v1.MenuCreateReq) (res *v1.MenuCreateRes, err error) {
	err = service.Menu().Store(ctx, model.MenuCreateInput{
		Pid:      req.Pid,
		Title:    req.Title,
		Slug:     req.Slug,
		Type:     req.Type,
		Icon:     req.Icon,
		IconType: req.IconType,
		Path:     req.Path,
		Order:    req.Order,
		Hidden:   req.Hidden,
	})
	return
}

func (m *cMenu) Update(ctx context.Context, req *v1.MenuUpdateReq) (res *v1.MenuUpdateRes, err error) {
	err = service.Menu().Update(ctx, req.Id, do.AdminMenu{
		Pid:      req.Pid,
		Title:    req.Title,
		Slug:     req.Slug,
		Type:     req.Type,
		Icon:     req.Icon,
		IconType: req.IconType,
		Path:     req.Path,
		Order:    req.Order,
		Hidden:   req.Hidden,
	})
	return
}

func (m *cMenu) Delete(ctx context.Context, req *v1.MenuDeleteReq) (res *v1.MenuDeleteRes, err error) {
	err = service.Menu().Delete(ctx, req.Id)
	return
}
